package com.ruoyi.web.controller.tool;

import com.deepoove.poi.data.Pictures;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class ReportUtils {

    //获取月末日期
    public static String getMonthEnd(String yearMonth) {
        int days = 0;
        String[] dateStr = yearMonth.split("-");
        String yearStr = dateStr[0];
        String monthStr = dateStr[1];
        int year = Integer.valueOf(yearStr);
        if (monthStr.equals("02")) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
        } else if (monthStr.equals("04") || monthStr.equals("06") || monthStr.equals("09") || monthStr.equals("11")) {
            days = 30;
        } else {
            days = 31;
        }
        return yearStr+"年"+monthStr+"月"+days+"日";
    }

    //获取月份区间
    public static String getDateRange(String yearMonth) {
        int days = 0;
        String[] dateStr = yearMonth.split("-");
        String yearStr = dateStr[0];
        String monthStr = dateStr[1];
        int year = Integer.valueOf(yearStr);
        if (monthStr.equals("02")) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
        } else if (monthStr.equals("04") || monthStr.equals("06") || monthStr.equals("09") || monthStr.equals("11")) {
            days = 30;
        } else {
            days = 31;
        }
        return yearStr+"年"+monthStr+"月01日至"+yearStr+"年"+monthStr+"月"+days+"日";
    }

    //格式化年月
    public static String getYearMonth(String yearMonth) {
        int days = 0;
        String[] dateStr = yearMonth.split("-");
        String yearStr = dateStr[0];
        String monthStr = dateStr[1];
        int year = Integer.valueOf(yearStr);
        if (monthStr.equals("02")) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                days = 29;
            } else {
                days = 28;
            }
        } else if (monthStr.equals("04") || monthStr.equals("06") || monthStr.equals("09") || monthStr.equals("11")) {
            days = 30;
        } else {
            days = 31;
        }
        return yearStr+"年"+monthStr+"月";
    }

    //获取文件的绝对路径
    public static String getFileAbsPath(String profilePath,String savePath) {
        String absPath = profilePath+"/upload/"+savePath.substring(16);
        return absPath;
    }

    //添加病害列表图片名称
    public static List<LinkedHashMap> addTableImgNames(List<LinkedHashMap> insDList) {
        int index = 0;
        for(int i=0;i<insDList.size();i++){
            if(insDList.get(i).get("diseaseImgs") != null && !insDList.get(i).get("diseaseImgs").equals("")) {
                index = index + 1;
                insDList.get(i).put("imgName","图"+index);

            }else{
                insDList.get(i).put("imgName","------");
            }
        }
        return insDList;
    }

    //获取病害图片的绝对路径列表
    public static List<LinkedHashMap> getFileNameAndAbsPath(String profilePath, List<LinkedHashMap> insDList) {
        List<LinkedHashMap>  linkedHashMapList = new ArrayList<LinkedHashMap>();
        int index = 0;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for(int i=0;i<insDList.size();i++){
            String absPath = "";
            if(insDList.get(i).get("diseaseImgs") != null && !insDList.get(i).get("diseaseImgs").equals("")) {
                String savePath = insDList.get(i).get("diseaseImgs").toString();
                String[] arr = savePath.split(",");
                savePath = arr[0];
                index = index + 1;
                if(index % 2 != 0){
                    linkedHashMap.put("imgName1","图"+index);
                    absPath = profilePath + "/upload/" + savePath.substring(16);
                    linkedHashMap.put("imgPath1", Pictures.ofLocal(absPath).size(310,250).create());
                    if(insDList.size()-1==i){
                        linkedHashMapList.add(linkedHashMap);
                    }
                }else{
                    linkedHashMap.put("imgName2","图"+index);
                    absPath = profilePath + "/upload/" + savePath.substring(16);
                    linkedHashMap.put("imgPath2", Pictures.ofLocal(absPath).size(310,250).create());
                    linkedHashMapList.add(linkedHashMap);
                    linkedHashMap = new LinkedHashMap();
                }
            }
        }
        return linkedHashMapList;
    }

    //添加维修列表图片名称
    public static List<LinkedHashMap> addMntTableImgNames(List<LinkedHashMap> mntList) {
        int index = 0;
        for(int i=0;i<mntList.size();i++){
            if(mntList.get(i).get("maintainImgs") != null && !mntList.get(i).get("maintainImgs").equals("")) {
                index = index + 1;
                mntList.get(i).put("imgName","图"+index);

            }else{
                mntList.get(i).put("imgName","------");
            }
        }
        return mntList;
    }

    //获取维修图片的绝对路径列表
    public static List<LinkedHashMap> getMntFileNameAndAbsPath(String profilePath, List<LinkedHashMap> mntList) {
        List<LinkedHashMap>  linkedHashMapList = new ArrayList<LinkedHashMap>();
        int index = 0;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for(int i=0;i<mntList.size();i++){
            String absPath = "";
            if(mntList.get(i).get("maintainImgs") != null && !mntList.get(i).get("maintainImgs").equals("")) {
                String savePath = mntList.get(i).get("maintainImgs").toString();
                String[] arr = savePath.split(",");
                savePath = arr[0];
                index = index + 1;
                if(index % 2 != 0){
                    linkedHashMap.put("imgName1","图"+index);
                    absPath = profilePath + "/upload/" + savePath.substring(16);
                    linkedHashMap.put("imgPath1", Pictures.ofLocal(absPath).size(310,250).create());
                    if(mntList.size()-1==i){
                        linkedHashMapList.add(linkedHashMap);
                    }
                }else{
                    linkedHashMap.put("imgName2","图"+index);
                    absPath = profilePath + "/upload/" + savePath.substring(16);
                    linkedHashMap.put("imgPath2", Pictures.ofLocal(absPath).size(310,250).create());
                    linkedHashMapList.add(linkedHashMap);
                    linkedHashMap = new LinkedHashMap();
                }
            }
        }
        return linkedHashMapList;
    }


    //获取年份区间
    public static String getDateYearRange(String year) {
        return year+"年01月01日至"+year+"年12月31日";
    }
    //获取文件的绝对路径列表
//    public static List<LinkedHashMap<String, Object>> getFileNameAndAbsPath(String profilePath, List<LinkedHashMap> insDList) {
//        List<LinkedHashMap<String, Object>>  linkedHashMapList = new ArrayList<LinkedHashMap<String, Object>>();
//        int index = 0;
//        for(int i=0;i<insDList.size();i++){
//            LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<String, Object>();
//            String absPath = "";
//            if(insDList.get(i).get("diseaseImgs") != null && !insDList.get(i).get("diseaseImgs").equals("")) {
//                String savePath = insDList.get(i).get("diseaseImgs").toString();
//                index = index + 1;
//                linkedHashMap.put("imgName","图"+index);
//                absPath = profilePath + "/upload/" + savePath.substring(16);
//                linkedHashMap.put("imgPath", Pictures.ofLocal(absPath).size(310,250).create());
//                linkedHashMapList.add(linkedHashMap);
//            }
//        }
//        return linkedHashMapList;
//    }
}
